<template>
  <div>
    <el-button type="primary" @click="handlerLogin" v-if="bl==null">测试登录</el-button>
    <el-button type="primary" @click="login" v-if="bl==null">请登录</el-button>
    <el-button type="danger" @click="handlerUser">测试获取用户请求</el-button>
    <el-button type="danger" @click="handlerLogout" v-if="bl!=null">测试注销</el-button>
  </div>
</template>
<script setup>
import requestUtil from "@/utils/request";
import store from "@/store";
import router from "@/router";
import {ref} from "vue";
import {inject} from "vue-demi";

const reload = inject('reload');

const handlerLogin = async () => {
  let result = await requestUtil.post("user/login", {"userName": "admin", "password": "wyb12345"});
  let data = result.data;
  if (data.code == 200) {
    const token = data.data.token;
    console.log("登录成功,token=" + token);
    store.commit("SET_TOKEN",token);
    reload();
  } else {
    console.log(data.code + " " + data.msg)
  }
}
let bl=ref(store.getters.GET_TOKEN)

const handlerUser = async () => {
  console.log(store==null);
  console.log(store);
  let result = await requestUtil.get("Test");
  let data = result.data;
  if (data.code == 200) {
    console.log(data.msg)
  } else {
    console.log(data.code + " " + data.msg)
  }
}
const login = async ()=>{
  await router.push({name:'login'})
}
const handlerLogout = async () => {
  let result = await requestUtil.get("user/logout");
  let data = result.data;
  if (data.code == 200) {
    console.log(data.msg)
    store.commit("DELETE_TOKEN")
    reload();
  }else {
    store.commit("DELETE_TOKEN")
    console.log(data.code + " " + data.msg)
    reload();
  }
}
</script>
